package com.weavernorth.salary;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

/**
 * ①一条流程的明细中一人只能有一条数据；②一个月内一个店铺只允许提交一条申请流程（通过店铺SAP编号判断）。
 */
public class CheckRepeatSalaryAction extends BaseBean implements Action {

    public static Log log = LogFactory.getLog(CheckRepeatSalaryAction.class.getName());

    /**
     * 终端店铺销售提成申请表 第一节点
     */
    @Override
    public String execute(RequestInfo request) {
        //数据操作
        RecordSet rs = new RecordSet();
        //当前表
        String strTable = request.getRequestManager().getBillTableName();
        //当前请求id
        String strRequestid = Util.null2String(request.getRequestid());
        log.info("流程id:" + strRequestid);
        //查询流程表里是否已存在当前店铺的信息
        String strSql = "select 1 from " + strTable + " fm,workflow_requestbase wr " +
                " where fm.requestid=wr.requestid and  substr(fm.sqsj, 0, 7) = (select substr(sqsj, 0, 7)    from " + strTable + " where requestid = ?)" +
                " and fm.requestid != ? and fm.dpbh=(select dpbh  from " + strTable + " where requestid = ?) and wr.currentnodetype!=0";
        log.info("查询sql:" + strSql);
        rs.executeQuery(strSql, strRequestid, strRequestid, strRequestid);
        if (rs.next()) {
            request.getRequestManager().setMessagecontent("本月有已本店铺的提成申请流程,只允许提交一条");
            return FAILURE_AND_CONTINUE;
        }

        //查询当前流程明细是否存在一个人多条
        String strDetilSql="select fmd.xm from "+strTable+" fm,"+strTable+"_dt1 fmd where fm.id=fmd.mainid and fm.requestid=? and fmd.xm is not null group by fmd.xm having count(1)>1";
       log.info("查询当前明细表一个人是否有多条"+strDetilSql);
        rs.executeQuery(strDetilSql,strRequestid);
        if(rs.next()){
            request.getRequestManager().setMessagecontent("当前流程明细中,一个人只能有一条数据");
            return FAILURE_AND_CONTINUE;
        }

        return SUCCESS;
    }
}
